//
//  PersonalViewModel.m
//  TestApplication
//
//  Created by Hanlingyu on 16/9/17.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "PersonalViewModel.h"
#import "TAMacro.h"
#import "PersonChannelImg.h"
#import "PersonChannelChannel.h"
#import "PersonChannelContent.h"
#import "PersonChannelPosts.h"
@implementation PersonalViewModel

- (void)setPersonalContent:(PersonChannelContent *)personalContent {
    _personalContent = personalContent;
    [self setUpTitleLabelFrame];
    [self setUpContentCollectionViewFrame];
    [self setUpMoreLabelFrame];
    [self setUpCellHeight];
    [self setUpBaceViewFrame];
}

// 标题的Frame
- (void)setUpTitleLabelFrame {
    CGFloat labelX = ViewMargin;
    CGFloat labelY = 0;
    CGFloat labelW = SCREEN_WIDTH - 4 * ViewMargin;
    CGFloat labelH = 40;
    _titleLabelFrame = CGRectMake(labelX, labelY, labelW, labelH);
}

// 图片的Frame
- (void)setUpContentCollectionViewFrame {
    
    if ([_personalContent.posts.firstObject.postType isEqualToString:@"Voice"]) {
        CGFloat height = 0;
        self.itemSizeArray = [NSMutableArray array];

        if (_personalContent.posts.count < 2) {
            CGFloat temp = 0;
            PersonChannelImg *img = _personalContent.posts.firstObject.img.firstObject;
            temp  = img.w / img.h;
            CGFloat lineHeight = (SCREEN_WIDTH - 40) / temp;
            CGFloat width = img.w / img.h * lineHeight;
            CGSize cellSize = CGSizeMake(width, lineHeight);
            NSString *cellSizeString = NSStringFromCGSize(cellSize);
            [_itemSizeArray addObject:cellSizeString];
            height = lineHeight;

        } else if (_personalContent.posts.count < 3) {
            CGFloat temp = 0;
            for (NSInteger i = 0; i < _personalContent.posts.count; i++) {
                PersonChannelImg *img = _personalContent.posts[i].img.firstObject;
                temp += img.w / img.h;
            }
            CGFloat lineHeight = (SCREEN_WIDTH - 42) / temp;
            for (NSInteger i = 0; i < _personalContent.posts.count; i++) {
                PersonChannelImg *img = _personalContent.posts[i].img.firstObject;
                CGFloat width = img.w / img.h * lineHeight;
                CGSize cellSize = CGSizeMake(width, lineHeight);
                NSString *cellSizeString = NSStringFromCGSize(cellSize);
                [_itemSizeArray addObject:cellSizeString];
                
            }
            height = lineHeight;

        } else if (_personalContent.posts.count < 4) {
            CGFloat firstTemp = 0;;
            CGFloat secTemp = 0;
            for (NSInteger i = 0; i < 2; i++) {
                PersonChannelImg *img = _personalContent.posts[i].img.firstObject;
                firstTemp += img.w / img.h;
            }
            CGFloat firstLineHeight = (SCREEN_WIDTH - 42) / firstTemp;
            for (NSInteger i = 0; i < 2; i++) {
                PersonChannelImg *img = _personalContent.posts[i].img.firstObject;
                CGFloat height = firstLineHeight;
                CGFloat width = img.w / img.h * firstLineHeight;
                CGSize cellSize = CGSizeMake(width, height);
                NSString *cellSizeString = NSStringFromCGSize(cellSize);
                [_itemSizeArray addObject:cellSizeString];
            }
            PersonChannelImg *img = _personalContent.posts[2].img.firstObject;
            secTemp  = img.w / img.h;
            CGFloat secLineHeight = (SCREEN_WIDTH - 40) / secTemp;
            CGFloat width = img.w / img.h * secLineHeight;
            CGSize cellSize = CGSizeMake(width, secLineHeight);
            NSString *cellSizeString = NSStringFromCGSize(cellSize);
            [_itemSizeArray addObject:cellSizeString];

            height = secLineHeight + firstLineHeight + 2;

        } else {
            CGFloat firstTemp = 0;;
            CGFloat secTemp = 0;
            for (NSInteger i = 0; i < 2; i++) {
                PersonChannelImg *img = _personalContent.posts[i].img.firstObject;
                firstTemp += img.w / img.h;
            }
            CGFloat firstLineHeight = (SCREEN_WIDTH - 42) / firstTemp;
            for (NSInteger i = 0; i < 2; i++) {
                PersonChannelImg *img = _personalContent.posts[i].img.firstObject;
                CGFloat height = firstLineHeight;
                CGFloat width = img.w / img.h * firstLineHeight;
                CGSize cellSize = CGSizeMake(width, height);
                NSString *cellSizeString = NSStringFromCGSize(cellSize);
                [_itemSizeArray addObject:cellSizeString];
            }
            
            for (NSInteger i = 2; i < 4; i++) {
                PersonChannelImg *img = _personalContent.posts[i].img.firstObject;
                secTemp += img.w / img.h;
                
            }
            CGFloat secLineHeight = (SCREEN_WIDTH - 42) / secTemp;
            
            for (NSInteger i = 2; i < 4; i++) {
                PersonChannelImg *img = _personalContent.posts[i].img.firstObject;
                CGFloat width = img.w / img.h * secLineHeight;
                CGFloat height = secLineHeight;
                CGSize cellSize = CGSizeMake(width, height);
                NSString *cellSizeString = NSStringFromCGSize(cellSize);
                [_itemSizeArray addObject:cellSizeString];
            }
            height = secLineHeight + firstLineHeight + 2;
        }
        
        CGFloat viewW = SCREEN_WIDTH - 40;
        CGFloat viewX = ViewMargin;
        CGFloat viewY = CGRectGetMaxY(_titleLabelFrame);
        _contentCollectionViewFrame = CGRectMake(viewX, viewY, viewW, height);
        

        
        
    } else {
        [self imageTpye];
    }
}

- (void)imageTpye {
    CGFloat height = 0;
    self.itemSizeArray = [NSMutableArray array];
    if (_personalContent.posts.count < 4) {
        CGFloat temp = 0;
        for (NSInteger i = 0; i < _personalContent.posts.count; i++) {
            PersonChannelImg *img = _personalContent.posts[i].img.firstObject;
            temp += img.w / img.h;
            
        }
        CGFloat lineHeight = (SCREEN_WIDTH - 44) / temp;
        for (NSInteger i = 0; i < _personalContent.posts.count; i++) {
            PersonChannelImg *img = _personalContent.posts[i].img.firstObject;
            CGFloat width = img.w / img.h * lineHeight;
            CGSize cellSize = CGSizeMake(width, lineHeight);
            NSString *cellSizeString = NSStringFromCGSize(cellSize);
            [_itemSizeArray addObject:cellSizeString];
            
        }
        
        height = lineHeight;
        
    } else if (_personalContent.posts.count < 7) {
        CGFloat firstTemp = 0;;
        CGFloat secTemp = 0;
        for (NSInteger i = 0; i < 3; i++) {
            PersonChannelImg *img = _personalContent.posts[i].img.firstObject;
            firstTemp += img.w / img.h;
        }
        CGFloat firstLineHeight = (SCREEN_WIDTH - 44) / firstTemp;
        for (NSInteger i = 0; i < 3; i++) {
            PersonChannelImg *img = _personalContent.posts[i].img.firstObject;
            CGFloat height = firstLineHeight;
            CGFloat width = img.w / img.h * firstLineHeight;
            CGSize cellSize = CGSizeMake(width, height);
            NSString *cellSizeString = NSStringFromCGSize(cellSize);
            [_itemSizeArray addObject:cellSizeString];
        }
        
        for (NSInteger i = 3; i < _personalContent.posts.count; i++) {
            PersonChannelImg *img = _personalContent.posts[i].img.firstObject;
            secTemp += img.w / img.h;
            
        }
        CGFloat secLineHeight = (SCREEN_WIDTH - 44) / secTemp;
        
        for (NSInteger i = 3; i < _personalContent.posts.count; i++) {
            PersonChannelImg *img = _personalContent.posts[i].img.firstObject;
            CGFloat width = img.w / img.h * secLineHeight;
            CGFloat height = secLineHeight;
            CGSize cellSize = CGSizeMake(width, height);
            NSString *cellSizeString = NSStringFromCGSize(cellSize);
            [_itemSizeArray addObject:cellSizeString];
        }
        height = secLineHeight + firstLineHeight + 2;
        
    } else {
        CGFloat firstTemp = 0;;
        CGFloat secTemp = 0;
        for (NSInteger i = 0; i < 3; i++) {
            PersonChannelImg *img = _personalContent.posts[i].img.firstObject;
            firstTemp += img.w / img.h;
        }
        CGFloat firstLineHeight = (SCREEN_WIDTH - 44) / firstTemp;
        
        for (NSInteger i = 0; i < 3; i++) {
            PersonChannelImg *img = _personalContent.posts[i].img.firstObject;
            CGFloat height = firstLineHeight;
            CGFloat width = img.w / img.h * firstLineHeight;
            CGSize cellSize = CGSizeMake(width, height);
            NSString *cellSizeString = NSStringFromCGSize(cellSize);
            [_itemSizeArray addObject:cellSizeString];
        }
        
        for (NSInteger i = 3; i < 6; i++) {
            PersonChannelImg *img = _personalContent.posts[i].img.firstObject;
            secTemp += img.w / img.h;
            
        }
        CGFloat secLineHeight = (SCREEN_WIDTH - 44) / secTemp;
        
        for (NSInteger i = 3; i < 6; i++) {
            PersonChannelImg *img = _personalContent.posts[i].img.firstObject;
            CGFloat height = secLineHeight;
            CGFloat width = img.w / img.h * secLineHeight;
            CGSize cellSize = CGSizeMake(width, height);
            NSString *cellSizeString = NSStringFromCGSize(cellSize);
            [_itemSizeArray addObject:cellSizeString];
            
        }
        
        height = firstLineHeight + secLineHeight + 2;
        
        
    }
    
    CGFloat viewW = SCREEN_WIDTH - 40;
    CGFloat viewX = ViewMargin;
    CGFloat viewY = CGRectGetMaxY(_titleLabelFrame);
    _contentCollectionViewFrame = CGRectMake(viewX, viewY, viewW, height);
    
}


// 更多的Frame
- (void)setUpMoreLabelFrame {
    CGFloat labelX = ViewMargin;
    CGFloat labelY = CGRectGetMaxY(_contentCollectionViewFrame);
    CGFloat labelW = SCREEN_WIDTH - 40;
    CGFloat labelH = 40;
    _moreLabelFrame = CGRectMake(labelX, labelY, labelW, labelH);
}

// cell高
- (void)setUpCellHeight {
    _cellHeight = CGRectGetMaxY(_moreLabelFrame) + ViewMargin;
}

// 背景View的Frame
- (void)setUpBaceViewFrame {
    CGFloat viewX = ViewMargin;
    CGFloat viewY = 0;
    CGFloat viewW = SCREEN_WIDTH - 20;
    CGFloat viewH = _cellHeight;
    _backViewFrame = CGRectMake(viewX, viewY, viewW, viewH);
}



@end
